/**
 * Created by ydz on 15/10/29.
 */
$(document).ready(function(){
    picCarousel();
    tabChange();
    menuFixed('scroll-footer');
    //menuFixedS('scroll-footer');
    //form11();
    /********************
     * 解决IE8对:last-child为类选择器不兼容的问题
     *******************/
    $("#item-second ul li:last-child").css("marginRight","0px");
    $("#item-sixth ul li:last-child").css("marginRight","0px");
    /********************
     * 查看媒体报道锚点
     *******************/
    $(".item-sixth-button").click(function(e) {
        var tar = e.target,
            cls = tar.className;
        if (cls.indexOf('item-sixth-button') > -1) {
            document.title = tar.innerHTML;
            location.href = tar.getAttribute('href');
        }
    });
    var doc=document,inputs=doc.getElementsByTagName('input'),supportPlaceholder='placeholder'in doc.createElement('input'),placeholder=function(input){var text=input.getAttribute('placeholder'),defaultValue=input.defaultValue;
        if(defaultValue==''){
            input.value=text}
        input.onfocus=function(){
            if(input.value===text){this.value=''}};
        input.onblur=function(){if(input.value===''){this.value=text}}};
    if(!supportPlaceholder){
        for(var i=0,len=inputs.length;i<len;i++){var input=inputs[i],text=input.getAttribute('placeholder');
            if(input.type==='text'&&text){placeholder(input)
            }
        }
    }
});
// 修正FF浏览器兼容
function getOffset(e) {
    var target = e.target;
    if (target.offsetLeft == undefined) {
        target = target.parentNode;
    }
    var pageCoord = getPageCoord(target);
    var eventCoord = {
        x: window.pageXOffset + e.clientX,
        y: window.pageYOffset + e.clientY
    };
    var offset = {
        offsetX: eventCoord.x - pageCoord.x,
        offsetY: eventCoord.y - pageCoord.y
    };
    return offset;
}

function getPageCoord(element) {
    var coord = {
        x: 0,
        y: 0
    };
    while (element) {
        coord.x += element.offsetLeft;
        coord.y += element.offsetTop;
        element = element.offsetParent;
    }
    return coord;
}
/********************
 * teacher
 *******************/
function picCarousel(){
    $(".items .img").hover(function(e){
        var _this  = $(this), //闭包
            _desc  = _this.find(".desc").stop(true),
            width  = _this.width(), //取得元素宽
            height = _this.height(), //取得元素高
            left   = (e.offsetX == undefined) ? getOffset(e).offsetX : e.offsetX, //从鼠标位置，得到左边界，利用修正ff兼容的方法
            top    = (e.offsetY == undefined) ? getOffset(e).offsetY : e.offsetY, //得到上边界
            right  = width - left, //计算出右边界
            bottom = height - top, //计算出下边界
            rect   = {}, //坐标对象，用于执行对应方法。
            _min   = Math.min(left, top, right, bottom), //得到最小值
            _out   = e.type == "mouseleave", //是否是离开事件
            spos   = {}; //起始位置

        rect[left] = function (epos){ //鼠从标左侧进入和离开事件
            spos = {"left": -width, "top": 0};
            if(_out){
                _desc.animate(spos, "fast"); //从左侧离开
            }else{
                _desc.css(spos).animate(epos, "fast"); //从左侧进入
            }
        };

        rect[top] = function (epos) { //鼠从标上边界进入和离开事件
            spos = {"top": -height, "left": 0};
            if(_out){
                _desc.animate(spos, "fast"); //从上面离开
            }else{
                _desc.css(spos).animate(epos, "fast"); //从上面进入
            }
        };

        rect[right] = function (epos){ //鼠从标右侧进入和离开事件
            spos = {"left": left,"top": 0};
            if(_out){
                _desc.animate(spos, "fast"); //从右侧成离开
            }else{
                _desc.css(spos).animate(epos, "fast"); //从右侧进入
            }
        };

        rect[bottom] = function (epos){ //鼠从标下边界进入和离开事件
            spos = {"top": height, "left": 0};
            if(_out){
                _desc.animate(spos, "fast"); //从底部离开
            }else{
                _desc.css(spos).animate(epos, "fast"); //从底部进入
            }
        };

        rect[_min]({"left":0, "top":0}); // 执行对应边界 进入/离开 的方法

    });
}
function tabChange(){
    // 标签的索引
    var index=0;
    var timer=null;

    var lis=$('#menuList li'),
        divs=$('.pic-content div');

    if(lis.length!=divs.length) return;

    // 遍历所有的页签
    for(var i=0;i<lis.length;i++){
        lis[i].id=i;
        lis[i].onmouseover=function(){
            // 用that这个变量来引用当前滑过的li
            var that=this;
            // 如果存在准备执行的定时器，立刻清除，只有当前停留时间大于100ms时才开始执行
            if(timer){
                clearTimeout(timer);
                timer=null;
            }
            // 延迟0.1秒执行
            timer=window.setTimeout(function(){
                for(var j=0;j<lis.length;j++){
                    lis[j].className='';
                    divs[j].style.display='none';
                }
                lis[that.id].className='active';
                divs[that.id].style.display='block';
            },100);
        }
    }
}
/********************
 * 学员心声 切换
 *******************/
$('#scroll').owlCarousel({
    items: 3,
    autoPlay: true,
    pagination:false,
    navigation: true,
    navigationText: ["",""],
    stopOnHover:true,
    scrollPerPage: false
});
$('#media-report').owlCarousel({
    items: 5,
    autoPlay: true,
    pagination:false,
    navigation: true,
    navigationText: ["",""],
    stopOnHover:true,
    scrollPerPage: false
});
/********************
 * 初始化页面时底层咨询注册栏滑动
 *******************/
function menuFixedS(id){
    var obj = document.getElementById(id);
    var _getHeight = obj.offsetTop;
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
    var H = getScrollTop()+getClientHeight()-100;
    if (H < _getHeight){
        obj.style.position = 'fixed';
        obj.style.zIndex = 999;
        obj.style.bottom = 0;
    }else{
        obj.style.position = 'relative';
    }
}
/********************
 * 底层咨询注册栏滑动
 *******************/
function menuFixed(id){
    var obj = document.getElementById(id);
    var _getHeight = obj.offsetTop;
    var scrollTop = document.documentElement.scrollTop || document.body.scrollTop
    window.onscroll = function(){
        //alert(id);
        var H = getScrollTop()+getClientHeight()-200;
        //alert(H);
        if (H < _getHeight && 700< H){
            //alert(1);
            obj.style.position = 'fixed';
            obj.style.zIndex = 999;
            obj.style.bottom = 0;
        }else{
            obj.style.position = 'relative';
        }
    }
}
$("#qq-content").click(function(){
    window.open('http://wpa.qq.com/msgrd?v=3&uin=3340419586&site=qq&menu=yes');
});
$(".item-button").click(function(){
    layer.open({
        type: 1,
        title: ['课程名额限量申请中', 'font-family:微软雅黑;'],
        skin: 'layui-layer-rim', //加上边框
        area: ['420px', '350px'], //宽高
        closeBtn:1,
        content: $('#layer-content')
    });
});

$("#qq").mouseover(function(){
    $(this).hide();
    $("#qq-content").animate({opacity: 'show'}, 100);
});
$("#qq-content").mouseout(function(){
    $(this).hide();
    $("#qq").show();
});
$("#wechat").mouseover(function(){
    $(this).hide();
    $("#wechat-content").animate({opacity: 'show'}, 100);
});
$("#wechat-content").mouseout(function(){
    $(this).hide();
    $("#wechat").show();
});
/********************
 * 取窗口滚动条高度
 ******************/
function getScrollTop()
{
    var scrollTop=0;
    if(document.documentElement&&document.documentElement.scrollTop)
    {
        scrollTop=document.documentElement.scrollTop;
    }
    else if(document.body)
    {
        scrollTop=document.body.scrollTop;
    }
    return scrollTop;
}
/********************
 * 取窗口可视范围的高度
 *******************/
function getClientHeight()
{
    var clientHeight=0;
    if(document.body.clientHeight&&document.documentElement.clientHeight)
    {
        var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
    }
    else
    {
        var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
    }
    return clientHeight;
}
/********************
 * 取文档内容实际高度
 *******************/
function getScrollHeight()
{
    return Math.max(document.body.scrollHeight,document.documentElement.scrollHeight);
}
/********************
 * 顶层微信/微博 弹框
 *******************/
$('#fa-weixin').hover(function(event){
    var offset = $(event.target).offset();
    $("#divPop").css({ top:  $(event.target).height() + 40 + "px", left: offset.left - 75+ "px"});
    $("#divPop").slideDown();
},function(){
    $("#divPop").slideUp();
});
/********************
 * 手机号码校验
 *******************/
function checkMobile(str) {
    var re = /^1\d{10}$/ ;
    if (re.test(str))
    {
        return true;
    }
    else {
        return false;
    }
}
/********************
 * 邮箱校验
 *******************/
function checkEmail(str){
    var re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/ ;
    if(re.test(str)){
            return true;
        }
    else {
            return false;
        }
}
/*banner表单校验*/
$("#name-input").blur(function(e){
    e=window.event||e;
    var eSrc=e.srcElement||e.target;
    var Xvalue=trim(this.value);
    if(isEmpty(Xvalue)){
        ShowMes(eSrc,"","wrong");
        $("#name-input-text").text("学生姓名不能为空");
    }else{
        ShowMes(eSrc,"","right");
        $("#name-input-text").text("");
  }
});
$("#telephone-input").blur(function(e){
    e=window.event||e;
    var eSrc=e.srcElement||e.target;
    var Xvalue=trim(this.value);
    if(!checkMobile(Xvalue)){
        ShowMes(eSrc,"","wrong");
        $("#telephone-input-text").text("手机号码格式不正确");
    }else{
        ShowMes(eSrc,"","right");
        $("#telephone-input-text").text("");
    }
});
$("#email-input").blur(function(e){
    e=window.event||e;
    var eSrc=e.srcElement||e.target;
    var Xvalue=trim(this.value);
    if(isEmpty(Xvalue)){
        return;
    }else if(!checkEmail(Xvalue)){
        ShowMes(eSrc,"","wrong");
        $("#email-input-text").text("邮箱格式不正确");
    }else{
        ShowMes(eSrc,"","right");
        $("#email-input-text").text("");
    }
});
$("#banner-input").click(function(){
    var name = $("#name-input").val();
    var telephone = $("#telephone-input").val();
    var email = $("#email-input").val();
    if(isEmpty(name)){
        //ShowMes(eSrc,"","wrong");
        $("#name-input-text").text("学生姓名不能为空");
        return;
    }else if(isEmpty(telephone)){
        //ShowMes(eSrc,"","wrong");
        $("#telephone-input-text").text("手机号码不能为空！");
        return;
    }else if(!checkMobile(telephone)){
        $("#telephone-input-text").text("手机号码格式不正确！");
        return;
    }else if(checkMobile(telephone)&&!isEmpty(name)){
        $.ajax({
            type: "GET",
            url: "http://vip.classba.com/crm/index.php?m=api&a=add_customer",
            data: {
                name:trim(name),
                telephone:telephone,
                email:email
            },
            dataType: "jsonp",
            jsonp:'callback',
            success: function(data){
                if(data.isSuccess){
                    layer.open({
                        type: 1,
                        title:'欢迎免费体验X+学习！',
                        skin: 'layui-layer-rim', //加上边框
                        area:["510px","330px"],
                        btn: ['确定'] ,
                        scrollbar: false,
                        content: $("#warn-info")
                    });
                }else{
                    layer.alert(data.error_code);
                }
            },
            error:function(XMLHttpRequest){
                layer.alert("请您核对申请人信息");
            }
        });
    }else{
        layer.alert("请您核对申请人信息");
    }
});
/*layer-banner表单校验*/
//$("#layer-name-input").blur(function(e){
//    e=window.event||e;
//    var eSrc=e.srcElement||e.target;
//    var Xvalue=trim(this.value);
//    if(isEmpty(Xvalue)){
//        layer.alert("学生姓名不能为空");
//    }else{
//        //ShowMes(eSrc,"","right");
//        //$("#layer-name-input-text").text("");
//    }
//});
//$("#layer-telephone-input").blur(function(e){
//    e=window.event||e;
//    var eSrc=e.srcElement||e.target;
//    var Xvalue=trim(this.value);
//    if(!checkMobile(Xvalue)){
//        layer.alert("手机号码格式不正确");
//    }else{
//        //ShowMes(eSrc,"","right");
//        //$("#layer-telephone-input-text").text("");
//    }
//});
//$("#layer-email-input").blur(function(e){
//    e=window.event||e;
//    var eSrc=e.srcElement||e.target;
//    var Xvalue=trim(this.value);
//    if(isEmpty(Xvalue)){
//        return;
//    }else if(!checkEmail(Xvalue)){
//        layer.alert("邮箱格式不正确");
//    }else{
//        //ShowMes(eSrc,"","right");
//        //$("#layer-email-input-text").text("");
//    }
//});
$("#layer-banner-input").click(function(){
    var name = $("#layer-name-input").val();
    var telephone = $("#layer-telephone-input").val();
    var email = $("#layer-email-input").val();
    if(isEmpty(name)){
        //ShowMes(eSrc,"","wrong");
        layer.alert("学生姓名不能为空！");
        return;
    }else if(isEmpty(telephone)){
        //ShowMes(eSrc,"","wrong");
        layer.alert("手机号码不能为空！");
        return;
    }else if(!checkMobile(telephone)){
        layer.alert("手机号码格式不正确！");
        return;
    }else if(checkMobile(telephone)&&!isEmpty(name)){
        $.ajax({
            type: "GET",
            url: "http://vip.classba.com/crm/index.php?m=api&a=add_customer",
            data: {
                name:trim(name),
                telephone:telephone,
                email:email
            },
            dataType: "jsonp",
            jsonp:'callback',
            beforeSend: function(){
                //layer.closeAll();
            },
            success: function(data){
                if(data.isSuccess){
                    layer.closeAll();
                    layer.open({
                        type: 1,
                        title:'欢迎免费体验X+学习！',
                        skin: 'layui-layer-rim', //加上边框
                        area:["510px","330px"],
                        btn: ['确定'] ,
                        scrollbar: false,
                        content: $("#warn-info")
                    });
                }else{
                    layer.alert(data.error_code);
                }
            },
            error:function(XMLHttpRequest){
                layer.alert("请您核对申请人信息");
            }
        });
    }else{
        layer.alert("请您核对申请人信息");
    }
});
/*判断为空*/
function isEmpty(o){
    return o==""?true:false;
}
/*去除空白字符*/
function trim(str){
        return str.replace(/(^\s*)|(\s*$)/g, "");
    }
function ShowMes(o,mes,type){
        if(!o.ele){
            var Xmes=document.createElement("div");
            document.body.appendChild(Xmes);
            o.ele=Xmes;
        }
        o.ele.className=type;
        o.ele.style.display="block";
        o.ele.style.left=(XgetPosition(o).x+280)+"px";
        o.ele.style.top=XgetPosition(o).y+7+"px";
        o.ele.innerHTML=mes;
}

function XgetPosition(e){
    var left = 0;
    var top  = 0;
    while(e.offsetParent){
        left += e.offsetLeft;
        top  += e.offsetTop;
        e= e.offsetParent;
    }
    left += e.offsetLeft;
    top  += e.offsetTop;
    return {
        x:left, y:top
    };
}
/*footer表单校验*/
//$("#footer-name-input").blur(function(e){
//    e=window.event||e;
//    var eSrc=e.srcElement||e.target;
//    var Xvalue=trim(this.value);
//    if(isEmpty(Xvalue)){
//        //ShowMes(eSrc,"","wrong");
//        layer.alert("学生姓名不能为空");
//    }else{
//        //ShowMes(eSrc,"","right");
//        //$("#name-input-text").text("");
//    }
//});
//$("#footer-telephone-input").blur(function(e){
//    e=window.event||e;
//    var eSrc=e.srcElement||e.target;
//    var Xvalue=trim(this.value);
//    if(!checkMobile(Xvalue)){
//        //ShowMes(eSrc,"","wrong");
//        layer.alert("手机号码格式不正确");
//    }else{
//        //ShowMes(eSrc,"","right");
//        //$("#telephone-input-text").text("");
//    }
//});
//$("#footer-email-input").blur(function(e){
//    e=window.event||e;
//    var eSrc=e.srcElement||e.target;
//    var Xvalue=trim(this.value);
//    if(isEmpty(Xvalue)){
//        return;
//    }else if(!checkEmail(Xvalue)){
//        //ShowMes(eSrc,"","wrong");
//        layer.alert("邮箱格式不正确");
//    }else{
//        //ShowMes(eSrc,"","right");
//        //$("#email-input-text").text("");
//    }
//});
$("#footer-button-submit").click(function(){
    var name = $("#footer-name-input").val();
    var telephone = $("#footer-telephone-input").val();
    var email = $("#footer-email-input").val();
    if(isEmpty(name)){
        //ShowMes(eSrc,"","wrong");
        layer.alert("学生姓名不能为空！");
        return;
    }else if(isEmpty(telephone)){
        //ShowMes(eSrc,"","wrong");
        layer.alert("手机号码不能为空！");
        return;
    }else if(!checkMobile(telephone)){
        layer.alert("手机号码格式不正确！");
        return;
    }else if(checkMobile(telephone)&&!isEmpty(name)){
        $.ajax({
            type: "GET",
            url: "http://vip.classba.com/crm/index.php?m=api&a=add_customer",
            data: {
                name:trim(name),
                telephone:telephone,
                email:email
            },
            dataType: "jsonp",
            jsonp:'callback',
            success: function(data){
                if(data.isSuccess){
                    //alert("申请成功！请保持手机畅通，等待课程顾问为您排课！");
                    //询问框
                    layer.open({
                        type: 1,
                        title:'欢迎免费体验X+学习！',
                        skin: 'layui-layer-rim', //加上边框
                        area:["510px","330px"],
                        btn: ['确定'] ,
                        scrollbar: false,
                        content: $("#warn-info")
                    });
                }else{
                    layer.alert(data.error_code);
                }
            },
            error:function(XMLHttpRequest){
                layer.alert("请您核对申请人信息");

            }
        });
    }else{
        layer.alert("请您核对申请人信息");
    }
});
function closeAll(){
    var layerObj = $('.xubox_layer');
    $.each(layerObj, function(){
        var i = $(thus).attr('times');
        layer.close(i);
    });
};